﻿using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using Swsk33.SimpleArcEngine.Dialog;
using System;
using System.Collections.Generic;

namespace Swsk33.SimpleArcEngine.Util
{
	/// <summary>
	/// 获取点击范围内的点要素
	/// </summary>
	public class PointBufferQuery : ICommand, ITool
	{
		/// <summary>
		/// 被操作地图对象
		/// </summary>
		private AxMapControl map;

		public void OnCreate(object Hook)
		{
			map = (AxMapControl)Hook;
		}

		public bool Enabled
		{
			get
			{
				return true;
			}
		}

		public bool Checked
		{
			get
			{
				return true;
			}
		}

		public string Name
		{
			get;
			set;
		}

		public string Caption
		{
			get;
			set;
		}

		public string Tooltip
		{
			get;
			set;
		}

		public string Message
		{
			get;
			set;
		}

		public string HelpFile
		{
			get;
			set;
		}

		public int HelpContextID
		{
			get;
			set;
		}

		public int Bitmap
		{
			get;
			set;
		}

		public string Category
		{
			get;
			set;
		}

		public void OnClick()
		{

		}

		public void OnMouseDown(int button, int shift, int x, int y)
		{
			// 获取鼠标在地图上点的点
			IGeometry point = map.ToMapPoint(x, y);
			// 以该点新建拓扑处理器
			ITopologicalOperator pointOperator = (ITopologicalOperator)point;
			pointOperator.Simplify();
			// 以该点建立一个缓冲区
			IGeometry selectBuffer = pointOperator.Buffer(8);
			// 新建查询器
			ISpatialFilter filter = new SpatialFilter();
			// 设定查询范围为全部字段
			filter.SubFields = "*";
			// 设定查询的几何的范围为我们缓冲区范围
			filter.Geometry = selectBuffer;
			// 查询Shape
			filter.GeometryField = "shape";
			// 查询相交于缓冲区的要素
			filter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
			List<string> result = new List<string>();
			for (int i = 0; i < map.LayerCount; i++)
			{
				IFeatureLayer layer = (IFeatureLayer)map.get_Layer(i);
				if (layer.FeatureClass.ShapeType != esriGeometryType.esriGeometryPoint)
				{
					continue;
				}
				IFeatureCursor cursor = layer.Search(filter, false);
				IFeature feature = null;
				while ((feature = cursor.NextFeature()) != null)
				{
					double distance = Math.Sqrt(Math.Pow(Math.Abs(((IPoint)point).X - ((IPoint)feature.Shape).X), 2) + Math.Pow(Math.Abs(((IPoint)point).Y - ((IPoint)feature.Shape).Y), 2));
					result.Add(feature.OID.ToString() + " - " + distance);
				}
			}
			// 进行排序
			string temp;
			for (int i = 0; i < result.Count - 1; i++)
			{
				for (int j = i; j < result.Count - i - 1; j++)
				{
					double current = double.Parse(result[j].Substring(result[j].LastIndexOf("-") + 2));
					double next = double.Parse(result[j + 1].Substring(result[j + 1].LastIndexOf("-") + 2));
					if (current > next)
					{
						temp = result[j];
						result[j] = result[j + 1];
						result[j + 1] = temp;
					}
				}
			}
			// 显示结果
			new ShowBufferPointDialog(result).ShowDialog();
		}

		public void OnMouseMove(int button, int shift, int x, int y)
		{

		}

		public void OnMouseUp(int button, int shift, int x, int y)
		{

		}

		public void OnDblClick()
		{

		}

		public void OnKeyDown(int keyCode, int shift)
		{

		}

		public void OnKeyUp(int keyCode, int shift)
		{

		}

		public bool OnContextMenu(int x, int y)
		{
			return true;
		}

		public void Refresh(int hdc)
		{

		}

		public bool Deactivate()
		{
			return true;
		}

		public int Cursor
		{
			get;
			set;
		}
	}
}